<template>
  <div class="Ad">
    <!-- 添加 -->
    <el-row :gutter="20" class="Ad-Info">
      <el-col :xs="6" :sm="4" :md="3" :lg="2" :xl="2" :offset="1">
        <el-button
          type="primary"
          class="Ad-Info-li"
          size="small"
          @click="AdShow = true"
          >{{ type == 0 ? "添 加" : "修 改" }}</el-button
        >
      </el-col>
    </el-row>
    <!-- 查询列表 -->
    <el-row class="Ad-list">
      <el-col :span="24">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="pic" label="图片" width="120">
            <template slot-scope="scope">
              <img :src="scope.row.pic" height="50" />
            </template>
          </el-table-column>

          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="selected = scope.row"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 添加或编辑关键词 -->
    <el-dialog
      :title="type == 0 ? '添加课程图' : '修改课程图'"
      :visible.sync="AdShow"
    >
      <el-form :model="AdForm" :rules="AdRules" ref="AdForm">
        <el-form-item label="图片" :label-width="AdWidth" prop="url">
          <el-upload
            :action="uploadbaseurl + '/upload'"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :limit="1"
            :on-remove="handleRemove"
            :on-success="uploadSuccess"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="AdSubmit('AdForm')" type="primary">{{
          type == 0 ? "添 加" : "修 改"
        }}</el-button>
      </div>
    </el-dialog>

    <!-- delect -->
    <el-dialog title="提示" :visible.sync="selected" width="30%">
      <span>确定删除?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="selected = null">取 消</el-button>
        <el-button
          type="danger"
          @click="deleteCourse(selected ? selected.id : null)"
          >删 除</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    var validatelinl = (rule, value, callback) => {
      let linlRG = /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/;
      if (value === "") {
        callback(new Error("请输入链接地址"));
      } else if (!linlRG.test(value)) {
        callback(new Error("请输入正确的网址"));
      } else {
        callback();
      }
    };
    return {
      selected: null,
      type: 0,
      uploadbaseurl: "",
      tableData: [],
      // 上传
      dialogImageUrl: "",
      dialogVisible: false,
      // 弹出层
      AdShow: false,
      AdForm: {
        pic: "",
        type: 1,
      },
      AdRules: {
        pic: [{ required: true, message: "请选择图片", trigger: "change" }],
      },
      AdWidth: "120px",
    };
  },
  created() {
    this.initData();
    this.uploadbaseurl = this.baseurl;
  },
  methods: {
    async initData() {
      this.AdShow = false;
      this.selected = null;
      let res = await this.http.post("/pic/queryPage", { query: {} });
      this.tableData = !res.error ? res.result.records : [];
      this.type = this.tableData.length != 0 ? 1 : 0;
      this.AdForm.id = this.tableData[0].id;
    },
    AdSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        console.log(this.AdForm);
        let param = { ...this.AdForm };
        if (valid) {
          let res = await this.http.post("/pic/addAndUpdatePic", param);
          if (!res.error) {
            this.eui.Message({ message: this.type==0?"添加成功":"修改成功", type: "success" });
            this.initData();
          } else {
            this.eui.Message({ message: res.msg, type: "error" });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    uploadSuccess(response, file, fileList) {
      this.AdForm.pic = response.result[0];
    },
    deleteCourse() {
      this.http.get("/pic/delete?id=" + this.selected.id).then((res) => {
        if (res.code == 200) {
          this.eui.Message({ message: "删除成功", type: "success" });
          this.selected = null;
          this.initData();
        } else {
          this.eui.Message({ message: res.msg, type: "error" });
        }
      });
    },
  },
};
</script>

<style scoped>
.Ad {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}
.Ad-Info {
  width: 100%;
  margin-bottom: 15px;
}
.Ad-Info-li {
  width: 100%;
}
.Ad-list {
  width: 100%;
  margin-bottom: 20px;
}
.Ad-page-box {
  display: flex;
  justify-content: flex-end;
}
</style>